package com.ruoyi.factorydata.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.factorydata.domain.FactoryData;
import com.ruoyi.factorydata.domain.WorkshopData;
import com.ruoyi.factorydata.domain.dto.ProductionLineDataDTO;
import com.ruoyi.factorydata.domain.vo.ProductionLineDataVo;
import com.ruoyi.factorydata.service.IFactoryDataService;
import com.ruoyi.factorydata.service.IWorkshopDataService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.factorydata.domain.ProductionLineData;
import com.ruoyi.factorydata.service.IProductionLineDataService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 生产线数据Controller
 * 
 * @author ruoyi
 * @date 2025-06-23
 */
@RestController
@RequestMapping("/productionlinedata/productionlinedata")
public class ProductionLineDataController extends BaseController
{
    @Autowired
    private IProductionLineDataService productionLineDataService;

    @Autowired
    private IWorkshopDataService workshopDataService;

    @Autowired
    private IFactoryDataService factoryDataService;
    /**
     * 查询生产线数据列表
     */
    @PreAuthorize("@ss.hasPermi('productionlinedata:productionlinedata:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody ProductionLineDataDTO productionLineDataDTO)
    {
        startPage();
        List<ProductionLineDataVo> list = productionLineDataService.selectProductionLineDataList(productionLineDataDTO);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }



    @GetMapping("/selectProductionLineByKeyword")
    public AjaxResult selectProductionLineByKeyword(@RequestParam(required = false) String keyword,
                                                    @RequestParam(required = false) Long workshopId) {
        startPage();
        List<ProductionLineData> list = productionLineDataService.list(new QueryWrapper<ProductionLineData>().lambda()
                .eq(workshopId != null, ProductionLineData::getWorkshopId, workshopId)
                .like(keyword != null && !keyword.isBlank(), ProductionLineData::getName, keyword));
        return AjaxResult.success(list);
    }
    /**
     * 导出生产线数据列表
     */
    @PreAuthorize("@ss.hasPermi('productionlinedata:productionlinedata:export')")
    @Log(title = "生产线数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductionLineDataDTO productionLineDataDTO)
    {
        List<ProductionLineDataVo> list = productionLineDataService.selectProductionLineDataList(productionLineDataDTO);
        ExcelUtil<ProductionLineDataVo> util = new ExcelUtil<ProductionLineDataVo>(ProductionLineDataVo.class);
        util.exportExcel(response, list, "生产线数据数据");
    }

    /**
     * 获取生产线数据详细信息
     */
//    @PreAuthorize("@ss.hasPermi('productionlinedata:productionlinedata:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ProductionLineData productionLineData = productionLineDataService.selectProductionLineDataById(id);
        WorkshopData workshopData = workshopDataService.selectWorkshopDataById(productionLineData.getWorkshopId());
        FactoryData factoryData = factoryDataService.selectFactoryDataById(workshopData.getFactoryId());
        ProductionLineDataVo productionLineDataVo = ProductionLineDataVo.builder()
                .id(productionLineData.getId())
                .name(productionLineData.getName())
                .productionLineCode(productionLineData.getProductionLineCode())
                .workshopId(workshopData.getId())
                .workshopName(workshopData.getName())
                .factoryId(factoryData.getId())
                .factoryName(factoryData.getFactoryName())
                .remarks(productionLineData.getRemarks())
                .delFlag(productionLineData.getDelFlag()).build();
        return AjaxResult.success(productionLineDataVo);
    }

    /**
     * 新增生产线数据
     */
    @PreAuthorize("@ss.hasPermi('productionlinedata:productionlinedata:add')")
    @Log(title = "生产线数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductionLineData productionLineData)
    {
        boolean exists =  productionLineDataService.lambdaQuery().eq(ProductionLineData::getProductionLineCode, productionLineData.getProductionLineCode()).count() > 0;
        if (exists){
            return AjaxResult.warn("生产线编码已存在");
        }
        productionLineData.setCreateBy(getUserId().toString());
        return toAjax(productionLineDataService.insertProductionLineData(productionLineData));
    }

    /**
     * 修改生产线数据
     */
    @PreAuthorize("@ss.hasPermi('productionlinedata:productionlinedata:edit')")
    @Log(title = "生产线数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductionLineData productionLineData)
    {
        List<Long> ids = productionLineDataService.lambdaQuery()
                .eq(ProductionLineData::getProductionLineCode, productionLineData.getProductionLineCode())
                .select(ProductionLineData::getId)
                .list().stream().map(ProductionLineData::getId).collect(Collectors.toList());
        boolean exists = !ids.isEmpty() && !ids.contains(productionLineData.getId());
        if (exists){
            return AjaxResult.warn("生产线编码已存在");
        }
        return toAjax(productionLineDataService.updateProductionLineData(productionLineData));
    }

    /**
     * 删除生产线数据
     */
    @PreAuthorize("@ss.hasPermi('productionlinedata:productionlinedata:remove')")
    @Log(title = "生产线数据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productionLineDataService.deleteProductionLineDataByIds(ids));
    }
}
